#include "stm32f10x.h"   // Device header
#include "OLED.h"
#include "Filter.h"
#define Lenth 128
#define Buffer_lenth 128*4
 uint16_t arr_pin0[Lenth] ;
 uint16_t arr_pin1[Lenth] ;
 uint16_t arr_pin2[Lenth] ;
 uint16_t arr_pin3[Lenth] ;
int num = 0;
uint16_t tem[Buffer_lenth]__attribute__((aligned(4)));  //4字节边界对齐（优化：参考豆包）
uint16_t* addr = tem;

void AD_Init(void)
{
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
		 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    
    TIM_InternalClockConfig(TIM3);
    
    
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    
    
    GPIO_InitTypeDef GPIO_Initstructure;
    GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AIN; 
    GPIO_Initstructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_2|GPIO_Pin_3;
    GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_Init(GPIOA, &GPIO_Initstructure);
	  
    GPIO_InitTypeDef GPIO2_Initstructure;
    GPIO2_Initstructure.GPIO_Mode = GPIO_Mode_AIN;  
    GPIO2_Initstructure.GPIO_Pin = GPIO_Pin_0;
    GPIO2_Initstructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_Init(GPIOB, &GPIO2_Initstructure);
  
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
		ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
    ADC_InitTypeDef ADC_InitStructure;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;  
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  // 软件触发  ：因为ADC上电的时候状态不确定，这样做是为了消除异常状态
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  
    ADC_InitStructure.ADC_NbrOfChannel = 4; 
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;  
    ADC_Init(ADC1, &ADC_InitStructure);
    
   
    DMA_InitTypeDef DMA_InitStructure;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; 
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)tem;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
    DMA_InitStructure.DMA_BufferSize = Buffer_lenth;  
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;  
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    DMA_Cmd(DMA1_Channel1, ENABLE); 
    
    
    ADC_Cmd(ADC1, ENABLE);  
    ADC_DMACmd(ADC1, ENABLE);  
  
 
    ADC_ResetCalibration(ADC1);
    while (ADC_GetResetCalibrationStatus(ADC1) == SET);  
    ADC_StartCalibration(ADC1);
    while (ADC_GetCalibrationStatus(ADC1) == SET);  
    
   
    ADC_SoftwareStartConvCmd(ADC1, ENABLE); 

    
    ADC_SoftwareStartConvCmd(ADC1, DISABLE);  
    
   
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; 
    ADC_Init(ADC1, &ADC_InitStructure);  
    ADC_ExternalTrigConvCmd(ADC1, ENABLE);  
    
  
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_Period = 99;  
    TIM_TimeBaseInitStructure.TIM_Prescaler = 3600 - 1; 
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
    
 
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    
 
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
  
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_Initstructure;
    NVIC_Initstructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_Initstructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_Initstructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_Initstructure);
    
  
    TIM_Cmd(TIM3, ENABLE);
}


void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
        

        addr = tem;
        for (num = 0; num < Lenth; num++)
        {
            arr_pin0[num] = Filter_Return1 (*addr) ; 
            arr_pin1[num] =Filter_Return2( *(addr + 1));   

					  arr_pin2[num] = Filter_Return3(*(addr + 2));
					  arr_pin3[num] =  Filter_Return4 (*(addr + 3));
            addr += 4;
        }
    }
}


uint16_t cha (uint16_t arr_pin0[] ,uint16_t arr_pin1[],int longest_time_last,int length)
{	
	int j = 0;
	int sum1 = 0;
	int sum2 = 0;
	int tau = 0;
	int final_tau = 0;
	for(tau=0 ;tau<longest_time_last;tau++)
	{
		sum1 = 0;
		
		 for(j=0;j<(length-tau);j++)
		{
			
			 sum1 = sum1 + arr_pin0[j]*arr_pin1[j+tau];
		   
		
		}
		if(sum1>sum2)
		{
			final_tau = tau;
		   sum2 = sum1 ;
			
		}
	}
	
	
	return final_tau;
	

}

